package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Menu> findMenu(String username) {
        List<Menu> menuList = new ArrayList<>();
        List<Role> roleList = roleDao.findRoleByUsername(username);
        for (Role role : roleList) {
            Integer id = role.getId();
            Set<Menu> menuSet = menuDao.findById(id);
            for (Menu menu : menuSet) {
                Integer level = menu.getLevel();
                if (level == 1){
                    menuList.add(menu);
                }
            }
            if (menuList != null){
                for (Menu menu1 : menuList) {
                    Integer id1 = menu1.getId();
                    List<Menu> childList = menuDao.findchildById(id1);
                    menu1.setChildren(childList);
                }
            }
        }
        return menuList;
    }
}
